/*
 *  Copyright 2011 Iurii Dunko, Iana Potochniak.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.yyminijava.syntaxtree;

import java.util.List;
import org.opu.yyminijava.syntaxtree.primary.Primary;
import org.opu.yyminijava.syntaxtree.primary.PrimaryMethodCall;
import org.opu.yyminijava.visitor.Visitor;


/**
 *  This instance using for method call and for assignment value to a variable.
 * Because of it the validation is responsibility of class instance not a
 * grammar and you need to call method {@link #validate()} after you build
 * this object.
 *  The validation check: if current instance is method call (don't have a
 * part with expression) - is it realy call of method
 *
 * @author Iurii Dunko
 * @author Iana  Potochniak
 */
public class MethodCallStatement implements Statement {

    private static final long serialVersionUID = 1323110697168L;

    private PostfixExpression postfixExpression;
    /** It can be null if this is call of method */
    private Expression expression;

    public MethodCallStatement() {
    }

    public MethodCallStatement(PostfixExpression postfixExpression, Expression expression) {
        this.postfixExpression = postfixExpression;
        this.expression = expression;
    }

    public Expression getExpression() {
        return expression;
    }

    public void setExpression(Expression expression) {
        this.expression = expression;
    }

    public PostfixExpression getPostfixExpression() {
        return postfixExpression;
    }

    public void setPostfixExpression(PostfixExpression postfixExpression) {
        this.postfixExpression = postfixExpression;
    }

    public void validate(){
        // postfixExpression: primary suffix *;
        // suffix: arrayAccess | methodInvocation | fieldInvocation;
        //   we expect 'methodInvocation' at the end if it method call

        // TODO: Create exception structure, don't use java exception
        // Using error is temporary solution, we use exception when the
        // incoming file contains errors, when the problem in our compiler
        // we use errors (but it for now)
        if (expression == null){
            List<Suffix> suffixs = postfixExpression.getSuffixs();
            Primary primary = postfixExpression.getPrimary();
            if (suffixs != null && !suffixs.isEmpty()){
                Suffix suffix = suffixs.get(suffixs.size() - 1);
                if (!(suffix instanceof MethodInvocation)){
                    throw new IllegalArgumentException("we expect method "
                             + "invocation in suffix but got - " + primary);
                }
            } else {
                if (primary == null){
                    throw new IllegalArgumentException("we expect method "
                            + "invocation but nothing found");
                }
                if(!(primary instanceof PrimaryMethodCall)){
                     throw new IllegalArgumentException("we expect method "
                             + "invocation but got - " + primary);
                }
            }
        }
    }

    @Override
    public String toString() {
        return String.valueOf(postfixExpression) +
                (expression != null? " = " + expression: "");
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(postfixExpression);
        if (expression != null) {
            visitor.visit(expression);
        }
    }
}
